#ifndef FLOWSCENE_H
#define FLOWSCENE_H

#include <QtGui>
#include <QtWidgets>
#include "define.h"
class FlowNode;
class NodeInfo;
class FlowScene : public QGraphicsScene
{
    Q_OBJECT
public:
    FlowScene();
    ~FlowScene();

    //上层调用清空场景，(会留下默认root)
    void resetItems();
    //返回代表导入的item数量，可能返回-1
    int loadItems(const QJsonArray& jArr,
                   const QString &rootID);
    //设置选中的对象. 不支持反选.
    void setSelected(const QString &id);

    QString getSelectedName() const;
    QString getSelectedId() const;
    QStringList getAllItemIds() const;
    QJsonObject getSelectedItemParam() const;
    QStringList getSelectedItemNext() const;
    void renameSelectedId(QString newid);
protected://约定为:纯粹功能，不组合
    //完全清理.
    void _clearItems();
    //设置选定的对象,id为空时是反选.
    void _setSelected(const QString &id);
    //对标准node进行扩展，创建出虚link和节点
    void _extendNode(NodeInfo* ninfo);
    void _pinchNode(NodeInfo* ninfo);

    QString _genNodeID(const QString &suffix) const;
    QTimer nodeAdjustTimer_;
    void activeNodeAdjust(); //触发节点自适应移动.

    void onClicked(FlowNode *node);
signals:
    void sigSelceted();
private:
    //虚拟根对象，画面中看不见
    NodeInfo *root_;
    QMap<QString, NodeInfo*> nodes_; //所有id-node索引.不包括虚拟根对象
    QList<NodeInfo*> voids_;  //选中产生的虚节点.
    NodeInfo *selected_;  //被选中的节点,也可能是虚节点.
    NodeInfo *focused_;   //被聚焦的节点,当选中虚节点,其父节点仍是focused.
protected:
    void touchPoints(QTouchEvent::TouchPoint touchPoint, QGraphicsItem* hit, qint64 time);
    /*************归并鼠标和触控消息******************/
    virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
    virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
    virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
    virtual bool event ( QEvent * event );
    virtual void touchEvent(QTouchEvent *event);
    void _touchEvent(QTouchEvent *event,bool bSimu);
    //void dotPress(int id, const QPointF &pos, qint64 time);
    //void dotMove(int id,const QPointF &pos, qint64 time);
    //void dotRelease(int id,const QPointF &pos, qint64 time);
    //为了更优雅的处理鼠标和触控模式，我做了个艰难的决定。二者只取其一，那么如何切换呢
    //在无touchdown的情况下，mousedown了N次，N=2,切换为鼠标；而只要有一次touch就切换为touch。初始为touch
    //逻辑：mTouchOrMouse为0，mousedown则++，touchdown则=0.当mTouchOrMouse>N则鼠标消息有效
    unsigned mTouchOrMouse;

    QHash<int,QGraphicsItem*> mId2Hit;
    /*************up归并鼠标和触控消息up******************/
    QElapsedTimer mElapsed;


};

#endif // FLOWSCENE_H
